package com.css.wc;

import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Tuple;

import java.util.HashMap;
import java.util.Map;

/**
 * 输入：单词及单词数显的次数
 * reduce
 */
public class WordCountBolt extends BaseRichBolt{
    private Map<String,Integer> wordCountMap=new HashMap<String,Integer>();
    @Override
    public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) {

    }

    @Override
    public void execute(Tuple tuple) {
        //获取单词信息及次数
        String word=tuple.getStringByField("word");
        String number=tuple.getStringByField("number");
        //定义Map记录单词数显次数
        if(wordCountMap.containsKey(word)){
            Integer old=wordCountMap.get("word");
            wordCountMap.put(word,old+Integer.parseInt(number));
        }else{
            wordCountMap.put(word,Integer.parseInt(number));
        }
        System.out.println(wordCountMap);
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {

    }
}
